package edu.iss.demo.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.model.Employee;
import edu.iss.demo.model.LeaveApplication;
import edu.iss.demo.service.EmployeeService;
import edu.iss.demo.service.LeaveApplicationService;

@Controller
public class MangerController {
	// Manager Navigation Pages
	@Autowired
	private LeaveApplicationService LAService;

	@RequestMapping(value = "/manager/detailApproveRejectLeave/{id}", method = RequestMethod.GET)
	public ModelAndView managerViewEmplLeave(@PathVariable int id) {
		System.out.println("id->" + id);
		ModelAndView mav = new ModelAndView("m_detailApproveRejectLeave");

		LeaveApplication Leave = LAService.findbyid(id);
		mav.addObject("Leave", Leave);
		return mav;
	}

	

	// Manager Logic Pages
	// manager new pages
	@RequestMapping(value = "/manager/ApproveRejectLeave/{id}", method = RequestMethod.GET)
	
	public ModelAndView ApproveRejectLeave(@PathVariable String id) {
		ModelAndView mav = new ModelAndView("m_ApproveRejectLeave");
		ArrayList<LeaveApplication> LeaveList = LAService.findEmployeeList(id);
		mav.addObject("LeaveList", LeaveList);
		return mav;
	}
	
	
	@RequestMapping(value = "/manager/viewAllEmpHistory/{id}", method = RequestMethod.GET)
	public ModelAndView Report(@PathVariable String id) {
		ModelAndView mav = new ModelAndView("m_viewAllEmpHistory");
		ArrayList<LeaveApplication> LeaveList = LAService.findEmployeeList(id);
		mav.addObject("LeaveList", LeaveList);
		return mav;
	}
	
	@RequestMapping(value = "/manager/Report", method = RequestMethod.GET)
	public ModelAndView ViewManagerHistory() {
		return new ModelAndView("m_reports");
		
	}
	@RequestMapping(value = "/manager/ApproveRejectLeave/{id}", method = RequestMethod.POST,params="update")
	public ModelAndView UpdateLeaveApplication(HttpServletRequest r,
			@ModelAttribute @Valid LeaveApplication Leave,
			BindingResult result, @PathVariable int id,
			final RedirectAttributes redirectAttributes) {
		Employee e = (Employee) r.getSession().getAttribute("loggedEmployee");
		ModelAndView mav = new ModelAndView(
				"redirect:/manager/ApproveRejectLeave/"+e.getEmployeeId()+".html");
		String message = "Leave was successfully updated.";
		
		Leave.setLeaveId(id);
		LAService.update(Leave);
		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	@RequestMapping(value = "/manager/ApproveRejectLeave/{id}", method = RequestMethod.POST,params="delete")
	public ModelAndView UpdateApplication(HttpServletRequest r,
			@ModelAttribute @Valid LeaveApplication Leave,
			BindingResult result, @PathVariable int id,
			final RedirectAttributes redirectAttributes) {
		Employee e = (Employee) r.getSession().getAttribute("loggedEmployee");
		ModelAndView mav = new ModelAndView(
				"redirect:/manager/ApproveRejectLeave/"+e.getEmployeeId()+".html");
		String message = "Leave was not successfully updated";
						Leave.setLeaveId(id);
		
		LAService.updateReject(Leave);
		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}

}
